package com.sxm.yiwei.tools;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sxm.yiwei.R;


/** 拨打电话工具类
 * Created by KuKa on 2017/9/15.
 */

public class CallPhone {
    /**
     * 拨号
     * @param context 上下文
     * @param phone 电话号码
     */
    public static void callPhone(final Context context, final String phone){
        final AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
        View view1 = LayoutInflater.from(context).inflate(R.layout.cancel_servicedialog, null);
        Button cancelbt1 = (Button) view1.findViewById(R.id.cancel_cancelbt);
        Button surebt1 = (Button) view1.findViewById(R.id.cancel_surebt);
        TextView t11 = (TextView) view1.findViewById(R.id.cancel_t1);
        TextView t21 = (TextView) view1.findViewById(R.id.cancel_t2);
        builder1.setView(view1);
        t21.setText(phone);
        surebt1.setText("呼叫");
        final AlertDialog alertDialog = builder1.create();
        cancelbt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        surebt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent callintent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;

                }
                context.startActivity(callintent);
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
    }
}
